﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace PO_2011
{
    public partial class ColorModForm : Form
    {
        private PictureBox imageBox;
        private Bitmap initImage;
        private bool savedChanges;

        public ColorModForm(PictureBox sourceImageBox)
        {
            InitializeComponent();

            imageBox = sourceImageBox;
            initImage = new Bitmap(sourceImageBox.Image);
            savedChanges = true;
        }

        private void brightnessSlider_ValueChanged(object sender, EventArgs e)
        {
            imageBox.Image = ColorConvert.Colors.changeBrightness(initImage, brightnessSlider.Value, brightnessSlider.Maximum);
            savedChanges = false;
        }

        private void contrastSlider_ValueChanged(object sender, EventArgs e)
        {
            imageBox.Image = ColorConvert.Colors.changeContrast(initImage, contrastSlider.Value, contrastSlider.Maximum);
            savedChanges = false;
        }

        private void gammaSlider_ValueChanged(object sender, EventArgs e)
        {
            imageBox.Image = ColorConvert.Colors.changeGamma(initImage, gammaSlider.Value);
            savedChanges = false;
        }

        private void brightnessSlider_MouseDown(object sender, MouseEventArgs e)
        {
            ImageHistory.History.addStep(new Bitmap(imageBox.Image));
        }

        private void contrastSlider_MouseDown(object sender, MouseEventArgs e)
        {
            ImageHistory.History.addStep(new Bitmap(imageBox.Image));
        }

        private void gammaSlider_MouseDown(object sender, MouseEventArgs e)
        {
            ImageHistory.History.addStep(new Bitmap(imageBox.Image));
        }

        private void brightnessResetButton_Click(object sender, EventArgs e)
        {
            ImageHistory.History.addStep(new Bitmap(imageBox.Image));
            
            brightnessSlider.Value = 0;
            savedChanges = false;
        }

        private void contrastResetButton_Click(object sender, EventArgs e)
        {
            ImageHistory.History.addStep(new Bitmap(imageBox.Image));
            
            contrastSlider.Value = 0;
            savedChanges = false;
        }

        private void gammaResetButton_Click(object sender, EventArgs e)
        {
            ImageHistory.History.addStep(new Bitmap(imageBox.Image));
            
            gammaSlider.Value = 9;
            savedChanges = false;
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            initImage = new Bitmap(imageBox.Image);
            savedChanges = true;
        }

        private void ColorModForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!savedChanges) imageBox.Image = initImage;
        }

    }
}
